package com.zxl.live.screen.ads.banner;

import android.content.Context;
import android.view.ViewGroup;

import com.google.android.gms.ads.AdSize;
import com.zxl.live.screen.ads.AdmobBannerHelper;

/**
 * Created by Administrator on 2018/2/12.
 */

public class AdmobBannerProxy extends AdmobBannerHelper {

    private boolean mIsLoaded;

    public AdmobBannerProxy(String type) {
        super(type, null);
    }

    @Override
    public void loadAds(Context context, AdSize adSize, String adId) {
        if (!mIsLoaded) {
            super.loadAds(context, adSize, adId);
        }
    }

    public boolean showAds(ViewGroup viewGroup) {
        if (mIsLoaded && mAdView != null) {
            try {
                if (ViewGroup.class.isInstance(mAdView.getParent())) {
                    ((ViewGroup) mAdView.getParent()).removeView(mAdView);
                }
                viewGroup.addView(mAdView);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public void onAdLoaded() {
        mIsLoaded = true;
        super.onAdLoaded();
    }

    @Override
    public void onAdFailedToLoad(int i) {
        mIsLoaded = false;
        super.onAdFailedToLoad(i);
    }

    @Override
    public void recycle() {
        mIsLoaded = false;
        super.recycle();
    }
}
